package com.xjl.example.commonUnsafe;

import com.xjl.annoations.ThreadSafe;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * 2019年12月25日10:51:12
 * DateTimeFormatter  是线程安全的
 */
@Slf4j
@ThreadSafe
public class DateformatExample2 {

    private final static int totalCount = 5000;
    private final static int threadCount =200;

    private static DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyyMMdd");

    public static void main(String[] args) throws InterruptedException {
        ExecutorService service = Executors.newCachedThreadPool();
        final Semaphore semaphore = new Semaphore(threadCount);
        final CountDownLatch count = new CountDownLatch(totalCount);
        for (int i = 0; i < totalCount; i++) {
            int finalI = i;
            service.execute(() -> {
                try {
                    semaphore.acquire();
                    format(finalI);
                    semaphore.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                count.countDown();
            });
        }
        count.await();
        service.shutdown();

    }

    private static void format(int i) {

        try {
            log.info("{}, {}", i, DateTime.parse("20180208", dateTimeFormatter).toDate());
        } catch (Exception e) {
            log.error("parse exception", e);
        }
    }


}
